package worldutils.huntgame.Listener;

import org.bukkit.*;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.server.ServerLoadEvent;
import org.bukkit.inventory.ItemStack;
import worldutils.huntgame.AutoStart.Timer;
import worldutils.huntgame.HuntGame;
import worldutils.huntgame.Utils.PlayerUtils;
import worldutils.huntgame.Utils.TagUtils;
import worldutils.huntgame.Utils.TimerUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

import static org.bukkit.Bukkit.getLogger;

public class mainListener implements Listener {
    @EventHandler
    public void onJoin(PlayerJoinEvent event){
        if (event.getPlayer().isOp()){
//            event.getPlayer().setGameMode(GameMode.CREATIVE);
            event.getPlayer().setGameMode(GameMode.SPECTATOR);
        } else {
            event.getPlayer().setGameMode(GameMode.SPECTATOR);
        }



        if (HuntGame.Gamestatus){
            TagUtils.addTag(event.getPlayer(),HuntGame.getMessage("spectator"));
        } else if (TagUtils.getTagCount(HuntGame.getMessage("hunter")) == TagUtils.getTagCount(HuntGame.getMessage("runner"))) {
            event.getPlayer().sendMessage(ChatColor.AQUA + HuntGame.getMessage("group-loot-consistent"));
            TagUtils.addTag(event.getPlayer(),TagUtils.getRandomTag());
        } else if (TagUtils.getTagCount(HuntGame.getMessage("hunter")) >= TagUtils.getTagCount(HuntGame.getMessage("runner"))) {
            event.getPlayer().sendMessage(ChatColor.AQUA + HuntGame.getMessage("group-loot").replace("*t",HuntGame.getMessage("runner")));
            TagUtils.addTag(event.getPlayer(),HuntGame.getMessage("runner"));
        } else if (TagUtils.getTagCount(HuntGame.getMessage("hunter")) <= TagUtils.getTagCount(HuntGame.getMessage("runner"))) {
            event.getPlayer().sendMessage(ChatColor.AQUA + HuntGame.getMessage("group-loot").replace("*t",HuntGame.getMessage("hunter")));
            TagUtils.addTag(event.getPlayer(),HuntGame.getMessage("hunter"));
        }

        if (HuntGame.Gamestatus){

        } else {
            int playerscount = TagUtils.getTagCount(HuntGame.getMessage("runner")) + TagUtils.getTagCount(HuntGame.getMessage("hunter"));
            if (playerscount == HuntGame.minautostartplayers || playerscount >= HuntGame.minautostartplayers) {
                if (Timer.getStats()){

                } else {
                    Timer.starttick();
                }
                //String.valueOf(HuntGame.minautostartplayers)
            } else {
                PlayerUtils.sendMessageToAllPlayer(HuntGame.getMessage("player-start-message").replace("*i",String.valueOf(HuntGame.minautostartplayers)));
            }
        }
    }
    @EventHandler
    public void onQuit(PlayerQuitEvent event){
        TagUtils.removeTag(event.getPlayer());
        if (HuntGame.Gamestatus) {
            if (TagUtils.getTagCount(HuntGame.getMessage("hunter")) == 0) {
                TimerUtils.stopgame(HuntGame.getMessage("quit-game-over").replace("*ft",HuntGame.getMessage("hunter")).replace("*wt",HuntGame.getMessage("runner")), HuntGame.getMessage("runner"));
            } else if (TagUtils.getTagCount(HuntGame.getMessage("runner")) == 0) {
                TimerUtils.stopgame(HuntGame.getMessage("quit-game-over").replace("*ft",HuntGame.getMessage("runner")).replace("*wt",HuntGame.getMessage("hunter")), HuntGame.getMessage("hunter"));
            }
        }
    }
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event){
        if (event.getEntity() instanceof Player && event.getDamager() instanceof Player){
            if (TagUtils.getTag((Player) event.getEntity()).equalsIgnoreCase(TagUtils.getTag((Player) event.getDamager()))){
                event.setCancelled(true);
            }
        }
    }
    @EventHandler
    public void onChat(AsyncPlayerChatEvent event){
        event.setFormat(TagUtils.getTag(event.getPlayer()) + " " + event.getPlayer().getName() + ": " + event.getMessage());
    }
    @EventHandler
    public void onDeath(PlayerDeathEvent event){
        if (HuntGame.Gamestatus){
            if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("runner"))){
                TagUtils.addTag(event.getPlayer(),HuntGame.getMessage("spectator"));
                event.getPlayer().sendMessage(ChatColor.RED + HuntGame.getMessage("death-to-spectator"));
                PlayerUtils.sendMessageToAllPlayer(HuntGame.getMessage("runner-death-message").replace("*t",HuntGame.getMessage("runner")).replace("*i",String.valueOf(TagUtils.getTagCount(HuntGame.getMessage("runner")))));
            }

            if (TagUtils.getTagCount(HuntGame.getMessage("runner")) == 0){
                TimerUtils.stopgame(HuntGame.getMessage("runner-all-death"),HuntGame.getMessage("hunter"));
            }
        }
    }
    @EventHandler
    public void onEnderDragonDeath(EntityDeathEvent event){
        if (event.getEntity() instanceof EnderDragon){
            if (HuntGame.Gamestatus){
                TimerUtils.stopgame(HuntGame.getMessage("enderdragon-death"), HuntGame.getMessage("runner"));
            }
        }
    }
    @EventHandler
    public void onMove(PlayerMoveEvent event){
        if (HuntGame.Gamestatus){
            if (HuntGame.HunterCantMove) {
                if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("hunter"))){
                    event.setCancelled(true);
//                    event.getPlayer().sendMessage(HuntGame.getMessage("cant-move-message").replace("*i",String.valueOf(HuntGame.runTime - TimerUtils.elapsedTime)));
                }
            }
        }
    }
    @EventHandler
    public void onDamage(EntityDamageEvent event){
        if (HuntGame.Gamestatus){
            if (HuntGame.HunterCantMove) {
                if (event.getEntity() instanceof Player) {
                    if (TagUtils.getTag((Player) event.getEntity()).equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                        event.setCancelled(true);
                    }
                }
            }
        }
    }
    @EventHandler
    public void onRespawn(PlayerRespawnEvent event){
        if (HuntGame.Gamestatus){
            if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("spectator"))) {
                event.getPlayer().setGameMode(GameMode.SPECTATOR);
            }
        }
    }
    @EventHandler
    public void onRespawn(PlayerChangedWorldEvent event){
        if (HuntGame.Gamestatus){
            if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("runner"))) {
                PlayerUtils.sendMessageToAllPlayer(HuntGame.getMessage("runner-change-world-message").replace("*p",event.getPlayer().getName()).replace("*w",event.getPlayer().getWorld().getName()));
            }

            if (HuntGame.endgameonenter){
                if (event.getPlayer().getWorld().getEnvironment() == World.Environment.THE_END){
                    TimerUtils.stopgame(HuntGame.getMessage("join-end-over-game").replace("*t",HuntGame.getMessage("runner")), HuntGame.getMessage("runner"));
                }
            }
        }
    }

    @EventHandler
    public void onDrop(PlayerDropItemEvent event){
        if (HuntGame.Gamestatus){
            if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                if (event.getItemDrop().getItemStack().getType() == Material.COMPASS) {
                    // 阻止物品丢弃
                    event.setCancelled(true);
                    // 发送信息给玩家
                    event.getPlayer().sendMessage(HuntGame.getMessage("drop-compass"));
                }
            }
        }
    }

    @EventHandler
    public void onRespawn2(PlayerRespawnEvent event){
        if (HuntGame.Gamestatus){
            if (TagUtils.getTag(event.getPlayer()).equalsIgnoreCase(HuntGame.getMessage("hunter"))) {
                ItemStack compass = new ItemStack(Material.COMPASS);
                event.getPlayer().getInventory().addItem(compass);
            }
        }
    }
}
